package cn.bopomo.dealer.base.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.util.ResultCode;
import cn.bopomo.dealer.base.util.SecurityUtil;
import cn.bopomo.dealer.base.vo.OperatorDetail;

public class EditPasswordAction extends BaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		
		String userId = this.getUserId(request);
		if (userId == null || userId.equals("")){
			//没有登录
			return this.findFailure(mapping);
		}
		
		IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
		OperatorDetail operator = os.getOperatorDetail(userId);
		String old_pwd = request.getParameter("old_pwd");
		old_pwd = SecurityUtil.MD5(old_pwd);
		if (!operator.getPassword().equalsIgnoreCase(old_pwd)){
			//跟原密码不相等
			return this.findFailure(mapping);
		}
		String new_pwd = request.getParameter("new_pwd");
		String confirm_pwd = request.getParameter("confirm_pwd");
		
		if (!new_pwd.equalsIgnoreCase(confirm_pwd)){
			//新密码与确认密码不一致
			return this.findFailure(mapping);
		}
		
		new_pwd = SecurityUtil.MD5(new_pwd);
		operator.setPassword(new_pwd);
		String result = os.modifyOperator(operator);
		
		if (result.equals(ResultCode.OP_SUCCESS)){
			return this.findSuccess(mapping);
		}else{
			//系统出错信息
			return this.findFailure(mapping);
		}
	
	}

}
